package com.meixiang.adapter.shopping;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.shopping.MallMainGoodsEntity;
import com.meixiang.recyclerview.OnItemClick;
import com.meixiang.tool.Tool;
import com.meixiang.util.GlideHelper;
import com.meixiang.util.TextViewUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 商品列表
 *
 * @author LZRUI
 */
public class GoodsListAdapter extends RecyclerView.Adapter<GoodsListAdapter.GoodsViewHolder> {

    private List<MallMainGoodsEntity> goodsList;
    private Context mContext;

    public GoodsListAdapter(Context context) {
        this.mContext = context;
    }

    /**
     * 刷新数据
     */
    public void refreshData(List<MallMainGoodsEntity> list) {
        if (list != null) {
            goodsList = list;
            notifyDataSetChanged();
        }
    }

    private OnItemClick itemClickListener;

    public void setOnItemClickListener(OnItemClick itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public GoodsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_goods, parent, false);
        return new GoodsViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final GoodsViewHolder holder, int position) {
        MallMainGoodsEntity goodsEntity = goodsList.get(position);
        if (goodsEntity == null) return;
        holder.tvGoodsName.setText(goodsEntity.getGoodsName());
        holder.tvNewPrice.setText(String.format(mContext.getString(R.string.price_str), goodsEntity.getGoodsStorePrice()));
        TextViewUtil.setStrickoutText(holder.tvOldPrice, String.format(mContext.getString(R.string.price_str), goodsEntity.getGoodsMarketPrice()));
        GlideHelper.showImage(mContext, goodsEntity.getGoodsImage(), holder.ivLogo);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Tool.isFastDoubleClick()) return;
                itemClickListener.onItemClick(holder.itemView, holder.getLayoutPosition());
            }
        });
    }

    @Override
    public int getItemCount() {
        return goodsList == null ? 0 : goodsList.size();
    }

    static class GoodsViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.tv_new_price)
        TextView tvNewPrice;
        @Bind(R.id.tv_old_price)
        TextView tvOldPrice;
        @Bind(R.id.tv_goods_name)
        TextView tvGoodsName;
        @Bind(R.id.iv_logo)
        ImageView ivLogo;

        public GoodsViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
